var MyTable={
    searchForm:"searchForm",
    searchUrl:"/log/list",
	tableId:"lgTable",
	table:null,
	prefix:"/log/"
};

$(function() {
    var defaultColunms = MyTable.initColumn();
    var table = new BSTable(MyTable.tableId, MyTable.searchUrl,defaultColunms);
	table.setQueryParams(getFormJson(MyTable.searchForm));
	table.setSortName("asc");
	table.setSortOrder("gmt_create");
	MyTable.table = table.init(0);
});
function reLoad() {
	MyTable.table.refresh(getFormJson(MyTable.searchForm));
}
function resetSearch(){
	cleanFormVal(MyTable.searchForm);
	MyTable.table.refresh(null);
}

MyTable.initColumn = function () {
    var columns =  [
		{checkbox : true},
		{field : 'id', title : '序号'},
		{field : 'userId',title : '用户id'},
		{field : 'username',title : '用户名'},
		{field : 'time',title : '时间'},
		{field : 'method',title : '方法',formatter : function(value, row, index) {
			if (value&&value.length>30) {
				return '<span title="'+value+'">'+value.substring(0,30)+'</span>';
			} else{
				return '<span>'+value+'</span>';
			}
		}},
		{field : 'module',title : '模块名称'},
		{field : 'ip',title : 'ip地址'},
		{field : 'type',title : '状态',align : 'center',
			formatter : function(value, row, index) {
				if (value == '1') {
					return '<span class="label label-danger">异常日志</span>';
				} else if (value == '0') {
					return '<span class="label label-primary">普通日志</span>';
				}
			}
		},
		{
			title : '操作',
			field : 'id',
			align : 'center',
			formatter : function(value, row, index) {
			var d = '<a class="btn btn-warning btn-sm ' + delBtn + '" href="#" title="删除"  mce_href="#" onclick="delRow(\''
				+ row.id
				+ '\')"><i class="fa fa-remove"></i></a> ';
			return d;
			}
		} ];
    return columns;
};



function delRow(logId){
    var config={
			url : MyTable.prefix+"/deleteRow",
			data:{"id":logId},
			sureDo:true,
			msg:"确定删除记录？"
	}
	EasyAjax.post_json(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			reLoad();
		} 
	});
}

function delRows(logId){
	var rows=checkSelect();
	if(!rows)
	return;
    var config={
			url : MyTable.prefix+"/deleteRows",
			data: {"ids" : rows},
			sureDo:true,
			msg:"确定删除记录？"
	}
	EasyAjax.post(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			reLoad();
		} 
	});
}

function checkSelect(){
	var rows = $('#'+MyTable.tableId).bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return null;
	}else{
		var ids = new Array();
		$.each(rows, function(i, row) {
			ids[i] = row.id;
		});
		return ids;
	}
}

